package com.chancein.cloud.report.charts.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import static com.chancein.cloud.report.charts.common.constant.CommonConstant.*;

/**
 * 文件处理的一些工具类
 *
 * @author: LiaoZiHao
 * @date: 2023/8/4 13:39
 * Copyright © 2022 Hundsun Technologies Inc. All Rights Reserved
 */
@Slf4j
public class FileTool {
    /**
     * 获取文件当前行数，用于重新启动不用手动判断行数
     *
     * @date 2023/8/4 13:15
     * @author liaozihao
     * @param fileName 文件名
     * @return {@link int}
     */
    public static int fileCurrentCount(String fileName) {
        String fullPath = FILE_SAVE_PATH + File.separator + fileName + ".json";
        File file = new File(fullPath);
        if (!file.exists()) {
            return 0;
        }
        log.info("[Restart]开始读取文件行数...");
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            int lineCount = 0;
            while (reader.readLine() != null) {
                lineCount++;
            }
            return lineCount;
        } catch (IOException e) {
            return 0;
        }
    }
}
